package com.ldj.hibernate;

import java.util.List;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;

import com.ldj.struts.form.EmployerInfoForm;
import com.ldj.struts.form.PetitionInfoForm;
import com.ldj.struts.form.UnitUserForm;

public class UnitUserDAO extends BaseHibernateDAO {
	public UnitUserDAO(HttpServletRequest request, HttpServletResponse response) {
		super(request, response);
		// TODO Auto-generated constructor stub
	}

	private static final Log log = LogFactory.getLog(UnitUserDAO.class);
	
	
	public UnitUser findId(java.lang.String id) {
		log.debug("getting UnitUser instance with id: " + id);
		try {
			UnitUser instance = (UnitUser) getSession().get(
					"com.ldj.hibernate.UnitUser", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}
	public List findAllPage(String areaCode,UnitUserForm uForm,String empInfoName) {
		try {
			String queryString = "from UnitUser where 1=1 ";
			if(areaCode != null && !"".equals(areaCode))
				queryString += " and unitUser.empInfoPlaceOrgan like '" + areaCode+ "%'";
			if(uForm.getUnitUserName() != null && !"".equals(uForm.getUnitUserName())){
				queryString += " and unitUserName like '%" + uForm.getUnitUserName() + "%'";
			}
			if(empInfoName != null && !"".equals(empInfoName)){
				queryString += " and unitUser.empInfoName like '%" + empInfoName + "%'";
			}
			queryString += " order by unitUserId desc";
			Query queryObject = getSession().createQuery(queryString);
			this.setPageSize(10);
			return this.jlist(queryObject);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	//查找已分配过单位的用户
	public List findAllUnit() {
		try {
			String queryString = "from UnitUser where unitUser!=null ";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	
	
	public void save(UnitUser transientInstance) {
		log.debug("saving UnitUser instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}
	
	public UnitUser merge(UnitUser detachedInstance) {
		try {
			UnitUser result = (UnitUser) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
	
	public void delete(UnitUser persistentInstance) {
		log.debug("deleting UnitUser instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}
	
	public List findAllfromEmployerInfoId(String empInfoId) {
		log.debug("finding all UnitUser instances");
		try {
			String queryString = "from UnitUser where unitUser.empInfoId='" + empInfoId + "'";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	//查找用户名为zhangye...的用户信息
	public int findUserNameMax(String userName) {
		log.debug("finding all UnitUser instances");
		Pattern pattern = Pattern.compile("[0-9]*"); 
		try {
			String queryString ="select max(substr(unitUserLoginName,8)) as t from UnitUser where unitUserLoginName like '" + userName + "____'";
			Query queryObject = getSession().createQuery(queryString);
			List list = queryObject.list();
			if(list != null && list.size() > 0){
				String code = (String)list.get(0);
					if(pattern.matcher(code).matches())
					{
						return Integer.parseInt(code);
					}
					return -1;
				}
			return 0;	
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
}